//
//  TicketCell.m
//  WeiYiMei
//
//  Created by 李叶夫 on 2019/4/14.
//  Copyright © 2019年 liyefu. All rights reserved.
//

#import "TicketCell.h"
#import "TicketEntity.h"

@interface TicketCell ()

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UILabel *ticketNameLab;// 唯艺美万科店
@property (nonatomic, strong) UILabel *ticketPriceLab;
@property (nonatomic, strong) UILabel *ticketDesLab;
@property (nonatomic, strong) UILabel *ticketReuseLab;
@property (nonatomic, strong) UILabel *ticketTimeLab;
//@property (nonatomic, strong) UILabel *ticketRangeLab;
@property (nonatomic, strong) UILabel *ticketId;// 券码
@property (nonatomic, strong) UILabel *belongConsumerNameLab;
@property (nonatomic, strong) UILabel *ticketNumLab;
@property (nonatomic, strong) UIImageView *stateIcon;
@property (nonatomic, strong) UIImageView *lineView;

@end

@implementation TicketCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupSubViews];
    }
    return self;
}

- (void)setEntity:(TicketEntity *)entity
{
    _entity = entity;
    self.ticketNameLab.text = String(entity.couponName);
    self.ticketPriceLab.text = [NSString stringWithFormat:@"￥%@",entity.money];
    self.ticketTimeLab.text = [NSString stringWithFormat:@"有效期：%@ 至 %@",entity.beginTime,entity.endTime];
    self.ticketDesLab.text = String(entity.summary);
    self.belongConsumerNameLab.text = [NSString stringWithFormat:@"所属商户：%@",String(entity.consumerName)];
    self.ticketId.text = [NSString stringWithFormat:@"券码：%@",entity.memberCouponCode];

    
    if (entity.state.intValue == 3) { // 已过期
        self.stateIcon.image = [UIImage imageNamed:@"q_yishiyong-1"];
        self.stateIcon.hidden = NO;
        NSDictionary *attri1 = @{NSForegroundColorAttributeName : kColorBBBBBB, NSFontAttributeName : [UIFont systemFontOfSize:13 weight:UIFontWeightMedium]};
        NSDictionary *attri2 = @{NSForegroundColorAttributeName : kColorBBBBBB, NSFontAttributeName : [UIFont systemFontOfSize:13 weight:UIFontWeightRegular]};
        NSMutableAttributedString *mString = [[NSMutableAttributedString alloc] initWithString:StringWithPlaceHolder(entity.unUseNum,@"0") attributes:attri1];
        [mString appendAttributedString:[[NSAttributedString alloc] initWithString:@"张可用/共" attributes:attri2]];
        [mString appendAttributedString:[[NSAttributedString alloc] initWithString:StringWithPlaceHolder(entity.num,@"0") attributes:attri1]];
        [mString appendAttributedString:[[NSAttributedString alloc] initWithString:@"张" attributes:attri2]];
        self.ticketNumLab.attributedText = mString;
    }
    else {
        if (entity.state.intValue == 2) { // 已使用
            self.stateIcon.image = [UIImage imageNamed:@"q_yishiyong"];
            self.stateIcon.hidden = NO;
        }
        else {
            self.stateIcon.hidden = YES;
        }
        NSDictionary *attri1 = @{NSForegroundColorAttributeName : ThemeColor, NSFontAttributeName : [UIFont systemFontOfSize:13 weight:UIFontWeightMedium]};
        NSDictionary *attri2 = @{NSForegroundColorAttributeName : ThemeColor, NSFontAttributeName : [UIFont systemFontOfSize:13 weight:UIFontWeightRegular]};
        NSDictionary *attri3 = @{NSForegroundColorAttributeName : kColor777777, NSFontAttributeName : [UIFont systemFontOfSize:13 weight:UIFontWeightRegular]};
        NSDictionary *attri4 = @{NSForegroundColorAttributeName : kColor333333, NSFontAttributeName : [UIFont systemFontOfSize:13 weight:UIFontWeightRegular]};
        NSDictionary *attri5 = @{NSForegroundColorAttributeName : kColor333333, NSFontAttributeName : [UIFont systemFontOfSize:13 weight:UIFontWeightMedium]};
        NSMutableAttributedString *mString = [[NSMutableAttributedString alloc] initWithString:StringWithPlaceHolder(entity.unUseNum,@"0") attributes:attri1];
        [mString appendAttributedString:[[NSAttributedString alloc] initWithString:@"张" attributes:attri2]];
        [mString appendAttributedString:[[NSAttributedString alloc] initWithString:@"可用" attributes:attri3]];
        [mString appendAttributedString:[[NSAttributedString alloc] initWithString:@"/" attributes:attri4]];
        [mString appendAttributedString:[[NSAttributedString alloc] initWithString:@"共" attributes:attri3]];
        [mString appendAttributedString:[[NSAttributedString alloc] initWithString:StringWithPlaceHolder(entity.num,@"0") attributes:attri5]];
        [mString appendAttributedString:[[NSAttributedString alloc] initWithString:@"张" attributes:attri3]];
        self.ticketNumLab.attributedText = mString;
    }
    
    [self setColor:entity.state.intValue];
    
    if (entity.reusable != nil && entity.reusable.intValue == 0) { // 显示
        self.ticketReuseLab.hidden = NO;
        [self.ticketReuseLab mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.ticketNameLab);
            make.top.equalTo(self.ticketDesLab.mas_bottom).offset(10);
            make.width.equalTo(@84);
            make.height.equalTo(@18);
        }];
        [self.ticketTimeLab mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.ticketReuseLab);
            make.left.equalTo(self.ticketReuseLab.mas_right).offset(6);
            make.right.lessThanOrEqualTo(self.bgView).offset(-15);
        }];
        
    }
    else {
        self.ticketReuseLab.hidden = YES;
        [self.ticketTimeLab mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.ticketDesLab.mas_bottom).offset(10);
            make.left.equalTo(self.ticketNameLab);
            make.right.lessThanOrEqualTo(self.bgView).offset(-15);
        }];
    }
}

- (void)setUseEntity:(TicketEntity *)useEntity
{
    _useEntity = useEntity;
    self.ticketNameLab.text = String(useEntity.couponName);
    self.ticketPriceLab.text = [NSString stringWithFormat:@"￥%@",useEntity.money];
    self.ticketTimeLab.text = [NSString stringWithFormat:@"使用日期：%@",String(useEntity.updateTime)];
    self.ticketDesLab.text = String(useEntity.summary);
    self.belongConsumerNameLab.text = [NSString stringWithFormat:@"所属商户：%@",String(useEntity.consumerName)];
    self.ticketId.text = [NSString stringWithFormat:@"券码：%@",useEntity.memberCouponCode];
    
    
    if (useEntity.state.intValue == 3) { // 已过期
        self.stateIcon.image = [UIImage imageNamed:@"q_yishiyong-1"];
        self.stateIcon.hidden = NO;
        NSDictionary *attri1 = @{NSForegroundColorAttributeName : kColorBBBBBB, NSFontAttributeName : [UIFont systemFontOfSize:13 weight:UIFontWeightMedium]};
        NSDictionary *attri2 = @{NSForegroundColorAttributeName : kColorBBBBBB, NSFontAttributeName : [UIFont systemFontOfSize:13 weight:UIFontWeightRegular]};
        NSMutableAttributedString *mString = [[NSMutableAttributedString alloc] initWithString:StringWithPlaceHolder(useEntity.useNum,@"0") attributes:attri1];
        [mString appendAttributedString:[[NSAttributedString alloc] initWithString:@"张已用/共" attributes:attri2]];
        [mString appendAttributedString:[[NSAttributedString alloc] initWithString:StringWithPlaceHolder(useEntity.num,@"0") attributes:attri1]];
        [mString appendAttributedString:[[NSAttributedString alloc] initWithString:@"张" attributes:attri2]];
        self.ticketNumLab.attributedText = mString;
    }
    else {
        if (useEntity.state.intValue == 2) { // 已使用
            self.stateIcon.image = [UIImage imageNamed:@"q_yishiyong"];
            self.stateIcon.hidden = NO;
        }
        else {
            self.stateIcon.hidden = YES;
        }
        NSDictionary *attri1 = @{NSForegroundColorAttributeName : ThemeColor, NSFontAttributeName : [UIFont systemFontOfSize:13 weight:UIFontWeightMedium]};
        NSDictionary *attri2 = @{NSForegroundColorAttributeName : ThemeColor, NSFontAttributeName : [UIFont systemFontOfSize:13 weight:UIFontWeightRegular]};
        NSDictionary *attri3 = @{NSForegroundColorAttributeName : kColor777777, NSFontAttributeName : [UIFont systemFontOfSize:13 weight:UIFontWeightRegular]};
        NSDictionary *attri4 = @{NSForegroundColorAttributeName : kColor333333, NSFontAttributeName : [UIFont systemFontOfSize:13 weight:UIFontWeightRegular]};
        NSDictionary *attri5 = @{NSForegroundColorAttributeName : kColor333333, NSFontAttributeName : [UIFont systemFontOfSize:13 weight:UIFontWeightMedium]};
        NSMutableAttributedString *mString = [[NSMutableAttributedString alloc] initWithString:StringWithPlaceHolder(useEntity.useNum,@"0") attributes:attri1];
        [mString appendAttributedString:[[NSAttributedString alloc] initWithString:@"张" attributes:attri2]];
        [mString appendAttributedString:[[NSAttributedString alloc] initWithString:@"已用" attributes:attri3]];
        [mString appendAttributedString:[[NSAttributedString alloc] initWithString:@"/" attributes:attri4]];
        [mString appendAttributedString:[[NSAttributedString alloc] initWithString:@"共" attributes:attri3]];
        [mString appendAttributedString:[[NSAttributedString alloc] initWithString:StringWithPlaceHolder(useEntity.num,@"0") attributes:attri5]];
        [mString appendAttributedString:[[NSAttributedString alloc] initWithString:@"张" attributes:attri3]];
        self.ticketNumLab.attributedText = mString;
    }
    
    [self setColor:useEntity.state.intValue];
    
    if (useEntity.reusable != nil && useEntity.reusable.intValue == 0) { // 显示
        self.ticketReuseLab.hidden = NO;
        [self.ticketReuseLab mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.ticketNameLab);
            make.top.equalTo(self.ticketDesLab.mas_bottom).offset(10);
            make.width.equalTo(@84);
            make.height.equalTo(@18);
        }];
        [self.ticketTimeLab mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.ticketReuseLab);
            make.left.equalTo(self.ticketReuseLab.mas_right).offset(6);
            make.right.lessThanOrEqualTo(self.bgView).offset(-15);
        }];
        
    }
    else {
        self.ticketReuseLab.hidden = YES;
        [self.ticketTimeLab mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.ticketDesLab.mas_bottom).offset(10);
            make.left.equalTo(self.ticketNameLab);
            make.right.lessThanOrEqualTo(self.bgView).offset(-15);
        }];
    }
}

- (void)setColor:(int)state
{
    if (state == 3) {
        self.ticketPriceLab.textColor = kColorBBBBBB;
        self.ticketDesLab.textColor = kColorBBBBBB;
        self.ticketTimeLab.textColor = kColorBBBBBB;
        self.belongConsumerNameLab.textColor = kColorBBBBBB;
        self.ticketId.textColor = kColorBBBBBB;
    }
    else {
        self.ticketPriceLab.textColor = UIColorFromRGB(0xF42E2E);
        self.ticketDesLab.textColor = kColor888888;
        self.ticketTimeLab.textColor = kColor888888;
        self.belongConsumerNameLab.textColor = kColor777777;
        self.ticketId.textColor = kColor777777;
    }
}

- (void)setupSubViews
{
    self.backgroundColor = kColorF2F2F2;
    
    self.bgView = [[UIView alloc] init];
    self.bgView.backgroundColor = [UIColor whiteColor];
    self.bgView.layer.cornerRadius = 5;
    self.bgView.layer.masksToBounds = YES;
    [self.contentView addSubview:self.bgView];
    
    self.stateIcon = [[UIImageView alloc] init];
    [self.bgView addSubview:self.stateIcon];
    
    self.ticketNameLab = [[UILabel alloc] init];
    self.ticketNameLab.font = [UIFont systemFontOfSize:17 weight:UIFontWeightMedium];
    self.ticketNameLab.numberOfLines = 0;
    [self.bgView addSubview:self.ticketNameLab];
    self.ticketNameLab.text = @"体验券";
    
    self.ticketPriceLab = [[UILabel alloc] init];
    self.ticketPriceLab.font = [UIFont systemFontOfSize:18 weight:UIFontWeightMedium];
    [self.bgView addSubview:self.ticketPriceLab];
    self.ticketPriceLab.text = @"￥0.00";
    [self.ticketPriceLab setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    
    self.ticketDesLab = [[UILabel alloc] init];
    self.ticketDesLab.font = [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
    self.ticketDesLab.numberOfLines = 0;
    [self.bgView addSubview:self.ticketDesLab];
    self.ticketDesLab.text = @"您可凭券在线下门店免费体验任意项目一次";
    
    self.ticketReuseLab = [[UILabel alloc] init];
    self.ticketReuseLab.font = [UIFont systemFontOfSize:10 weight:UIFontWeightRegular];
    self.ticketReuseLab.textColor = UIColorFromRGB(0xFF4D4D);
    self.ticketReuseLab.backgroundColor = UIColorFromRGB(0xFFF1F1);
    self.ticketReuseLab.layer.borderColor = UIColorFromRGB(0xFF6E6E).CGColor;
    self.ticketReuseLab.layer.borderWidth = 1;
    self.ticketReuseLab.layer.cornerRadius = 2;
    self.ticketReuseLab.layer.masksToBounds = YES;
    self.ticketReuseLab.text = @"每天仅使用一张";
    self.ticketReuseLab.textAlignment = NSTextAlignmentCenter;
    [self.bgView addSubview:self.ticketReuseLab];
    
    self.ticketTimeLab = [[UILabel alloc] init];
    self.ticketTimeLab.font = [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
    [self.bgView addSubview:self.ticketTimeLab];
    self.ticketTimeLab.text = @"有效期至：2019-12-31";
    self.ticketTimeLab.adjustsFontSizeToFitWidth = YES;
    self.ticketTimeLab.minimumScaleFactor = 0.5;
    
    self.belongConsumerNameLab = [[UILabel alloc] init];
    self.belongConsumerNameLab.font = [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
    self.belongConsumerNameLab.numberOfLines = 0;
    [self.bgView addSubview:self.belongConsumerNameLab];
    self.belongConsumerNameLab.text = @"所属商户：唯艺美健康管理中心";
    
    self.lineView = [[UIImageView alloc] init];
    self.lineView.image = [UIImage imageNamed:@"ticket_line"];
    [self.bgView addSubview:self.lineView];
    
    self.ticketNumLab = [[UILabel alloc] init];
    [self.bgView addSubview:self.ticketNumLab];
    
    self.ticketId = [[UILabel alloc] init];
    self.ticketId.font = [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
    self.ticketId.numberOfLines = 0;
    [self.bgView addSubview:self.ticketId];
    self.ticketId.text = @"券码：";
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(10);
        make.left.equalTo(self.contentView).offset(15);
        make.right.equalTo(self.contentView).offset(-15);
        make.bottom.equalTo(self.contentView);
    }];
    
    [self.ticketNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView).offset(16);
        make.left.equalTo(self.bgView).offset(15);
        make.right.lessThanOrEqualTo(self.ticketPriceLab.mas_left).offset(-10);
    }];
    
    [self.ticketPriceLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.ticketNameLab);
        make.right.equalTo(self.bgView).offset(-15);
    }];
    
    [self.ticketDesLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.ticketNameLab.mas_bottom).offset(9);
        make.left.equalTo(self.ticketNameLab);
        make.right.lessThanOrEqualTo(self.bgView).offset(-15);
    }];
    
    [self.ticketTimeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.ticketDesLab.mas_bottom).offset(10);
        make.left.equalTo(self.ticketNameLab);
        make.right.lessThanOrEqualTo(self.bgView).offset(-15);
    }];
    
    [self.belongConsumerNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.ticketTimeLab.mas_bottom).offset(25);
        make.left.equalTo(self.ticketNameLab);
        make.right.lessThanOrEqualTo(self.bgView).offset(-15);
    }];
    
    [self.ticketNumLab setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [self.ticketNumLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.belongConsumerNameLab.mas_bottom).offset(6);
        make.left.equalTo(self.ticketNameLab);
    }];
    
    [self.ticketId mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.ticketNumLab);
        make.left.equalTo(self.ticketNumLab.mas_right).offset(20);
        make.right.lessThanOrEqualTo(self.bgView).offset(-15);
        make.bottom.equalTo(self.bgView).offset(-10);
    }];
    
    [self.stateIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.bgView).offset(-20);
        make.bottom.equalTo(self.bgView).offset(-20);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.bgView);
        make.top.equalTo(self.ticketTimeLab.mas_bottom).offset(9);
    }];
}

@end
